﻿
Public Class UserControl1
    Public Property codice_comp As String
    Public Property quantita As String
    Dim RIGA_COMP_GESTITI = 0
    Dim DATASET_QUANTITA As New System.Data.DataSet

    Public Sub OTTIENI_QUANTITA()
        RIGA_COMP_GESTITI = 0
        Dim query As New Data.OleDb.OleDbCommand
        query.CommandText = ("SELECT * FROM COMP_NON_GESTITI WHERE CODICE ='" & codice_comp & "'")
        'Dim query = Clsdatabase.GENERA_QUERY("COMP_NON_GESTITI", codice)


        Dim DA As System.Data.OleDb.OleDbDataAdapter
        Dim CONNESSIONE As New Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\Lafer2k3\uff_tec\Dbporteclienti.mdb")
        DA = New Data.OleDb.OleDbDataAdapter(query.ToString, CONNESSIONE)
        DA.SelectCommand = query
        DATASET_QUANTITA.Clear()
        If DATASET_QUANTITA.Tables.Contains("QUANTITA") = False Then
            DATASET_QUANTITA.Tables.Add("QUANTITA")
        End If
        DA.SelectCommand.Connection = CONNESSIONE
        DA.Fill(DATASET_QUANTITA, "QUANTITA")
        Try
            Label7.Text = DATASET_QUANTITA.Tables("QUANTITA").Rows(0).Item(2)
            RIGA_COMP_GESTITI = 1
        Catch ex As IndexOutOfRangeException
            Label7.Text = "0"
        End Try
        quantita = Label7.Text
    End Sub

    Sub start()
        ricerca_completa()

        RIGA_COMP_GESTITI = 0
        Dim query As New Data.OleDb.OleDbCommand
        query.CommandText = ("SELECT * FROM COMP_NON_GESTITI WHERE CODICE ='" & codice_comp & "'")
        'Dim query = Clsdatabase.GENERA_QUERY("COMP_NON_GESTITI", codice)
        Dim DATASET_QUANTITA As New System.Data.DataSet


        Dim DA As System.Data.OleDb.OleDbDataAdapter
        Dim CONNESSIONE As New Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\Lafer2k3\uff_tec\Dbporteclienti.mdb")
        DA = New Data.OleDb.OleDbDataAdapter(query.ToString, CONNESSIONE)
        DA.SelectCommand = query
        DATASET_QUANTITA.Clear()
        DATASET_QUANTITA.Tables.Add("QUANTITA")
        DA.SelectCommand.Connection = CONNESSIONE
        DA.Fill(DATASET_QUANTITA, "QUANTITA")
        Try
            Label7.Text = DATASET_QUANTITA.Tables("QUANTITA").Rows(0).Item(2)
            RIGA_COMP_GESTITI = 1
        Catch ex As IndexOutOfRangeException
            Label7.Text = "0"
        End Try
        quantita = Label7.Text
    End Sub

    Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
        Dim quantita As String = TextBox2.Text

        If CBRAL.Text <> "" Then
            codice_comp = codice_comp.Insert(codice_comp.Length, "-" & CBRAL.Text)
        End If

        Dim query As New Data.OleDb.OleDbCommand
        Dim DA As System.Data.OleDb.OleDbDataAdapter
        Dim CONNESSIONE As New Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\Lafer2k3\uff_tec\Dbporteclienti.mdb")
        DA = New Data.OleDb.OleDbDataAdapter(query.ToString, CONNESSIONE)
        If Label7.Text = "0" And RIGA_COMP_GESTITI = 0 Then

            query.CommandText = "INSERT INTO COMP_NON_GESTITI(CODICE, QUANTITA) VALUES('" & codice_comp & "', " & quantita & ")"
            DA.InsertCommand = query
            DA.InsertCommand.Connection = CONNESSIONE
            CONNESSIONE.Open()
            DA.InsertCommand.ExecuteNonQuery()
            CONNESSIONE.Close()
            Label7.Text = quantita
            TextBox2.Text = ""

        ElseIf Label7.Text <> "0" Or Label7.Text = "0" And RIGA_COMP_GESTITI = 1 Then
            query.CommandText = "UPDATE COMP_NON_GESTITI SET QUANTITA= " & quantita & " WHERE CODICE= '" & codice_comp & "'"
            DA.UpdateCommand = query
            DA.UpdateCommand.Connection = CONNESSIONE
            CONNESSIONE.Open()
            DA.UpdateCommand.ExecuteNonQuery()
            CONNESSIONE.Close()
            Label7.Text = quantita
            TextBox2.Text = ""
        End If

    End Sub
   
    Private Sub UserControl1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       
        For Each ITEM In Clsdatabase.Lista_item("COLORI", "COLORE")
            CBRAL.Items.Add(ITEM)
        Next
        Label7.Text = ""
    End Sub

    
    Private Sub CBRAL_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CBRAL.TextChanged
        Dim CODICE = codice_comp
        If CBRAL.Text = "" Then
        Else
            CODICE = CODICE.Insert(CODICE.Length, "-" & CBRAL.Text)
        End If
        quantita_comp(CODICE)
    End Sub

    Public Sub quantita_comp(ByVal codice As String)
        RIGA_COMP_GESTITI = 0
        Dim query As New Data.OleDb.OleDbCommand
        query.CommandText = ("SELECT * FROM COMP_NON_GESTITI WHERE CODICE ='" & codice & "'")
        'Dim query = Clsdatabase.GENERA_QUERY("COMP_NON_GESTITI", codice)
        Dim DATASET_QUANTITA As New System.Data.DataSet


        Dim DA As System.Data.OleDb.OleDbDataAdapter
        Dim CONNESSIONE As New Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\Lafer2k3\uff_tec\Dbporteclienti.mdb")
        DA = New Data.OleDb.OleDbDataAdapter(query.ToString, CONNESSIONE)
        DA.SelectCommand = query
        DATASET_QUANTITA.Clear()
        DATASET_QUANTITA.Tables.Add("QUANTITA")
        DA.SelectCommand.Connection = CONNESSIONE
        DA.Fill(DATASET_QUANTITA, "QUANTITA")
        Try
            Label7.Text = DATASET_QUANTITA.Tables("QUANTITA").Rows(0).Item(2)
            RIGA_COMP_GESTITI = 1
        Catch ex As IndexOutOfRangeException
            Label7.Text = "0"
        End Try

    End Sub

    Sub ricerca_completa()
        ListBox1.Items.Clear()
        For Each item In CBRAL.Items
            Dim codice = codice_comp
            codice = codice.Insert(codice.Length, "-" & item)

            RIGA_COMP_GESTITI = 0
            Dim query As New Data.OleDb.OleDbCommand
            query.CommandText = ("SELECT * FROM COMP_NON_GESTITI WHERE CODICE ='" & codice & "'")
            'Dim query = Clsdatabase.GENERA_QUERY("COMP_NON_GESTITI", codice)
            Dim DATASET_QUANTITA As New System.Data.DataSet


            Dim DA As System.Data.OleDb.OleDbDataAdapter
            Dim CONNESSIONE As New Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\Lafer2k3\uff_tec\Dbporteclienti.mdb")
            DA = New Data.OleDb.OleDbDataAdapter(query.ToString, CONNESSIONE)
            DA.SelectCommand = query
            DATASET_QUANTITA.Clear()
            DATASET_QUANTITA.Tables.Add("QUANTITA")
            DA.SelectCommand.Connection = CONNESSIONE
            DA.Fill(DATASET_QUANTITA, "QUANTITA")
            Try
                ListBox1.Items.Add(item & " = " & DATASET_QUANTITA.Tables("QUANTITA").Rows(0).Item(2))
                RIGA_COMP_GESTITI = 1
            Catch ex As IndexOutOfRangeException
                ListBox1.Items.Add(item & " = 0")
            End Try
        Next

    End Sub

    Sub DATASET(ByVal DATASET As System.Data.DataSet)
        DATASET.Reset()
        Dim query As New Data.OleDb.OleDbCommand
        query.CommandText = ("SELECT * FROM COMP_NON_GESTITI")
        Dim DA As System.Data.OleDb.OleDbDataAdapter
        Dim CONNESSIONE As New Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\Lafer2k3\uff_tec\Dbporteclienti.mdb")
        DA = New Data.OleDb.OleDbDataAdapter(query.ToString, CONNESSIONE)
        DA.SelectCommand = query
        DATASET.Clear()
        DATASET.Tables.Add("QUANTITA")
        DA.SelectCommand.Connection = CONNESSIONE
        DA.Fill(DATASET, "QUANTITA")

    End Sub

    Private Sub TextBox2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Enter
        If codice_comp <> Nothing Then
            start()
        End If
    End Sub

    Public Function OTTIENI_QUANTITA(ByVal CODICE As String)
        Dim QUANTITA As String = 0


        Return QUANTITA
    End Function
End Class
